import { queryUserList } from './service';
import { logisticsCompanyList } from '@/utils/config';


const srcOptions = [
  { label: '手动录入', value: '0' },
  { label: '接口获取', value: '1' },
  { label: '仓储APP登记', value: '2' },
]

const expressOptions = logisticsCompanyList.map(v => {
  return {
    label: v, value: v
  }
})

//表单配置项
const formItemData = {
  'logisticsNo': {
    itemConfig: {
      name: 'logisticsNo'
    },
    elConfig: {
      formType: 'Input',
      placeholder: '物流单号',
    }
  },
  'logisticsCompany': {
    itemConfig: {
      name: 'logisticsCompany'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '物流公司',
      options: expressOptions
    }
  },
  'src': {
    itemConfig: {
      name: 'src'
    },
    elConfig: {
      formType: 'Select',
      placeholder: '数据获取方式',
      options: srcOptions
    }
  },
  'createAccount': {
    itemConfig: {
      name: 'createAccount'
    },
    elConfig: {
      formType: 'SearchSelect',
      placeholder: '创建人',
      fetchOptions: handleUserList,
    }
  },
  'time': {
    itemConfig: {
      name: 'time'
    },
    elConfig: {
      formType: 'rangTime',
      placeholder: ['创建开始时间', '创建结束时间'],
      prefix: '创建时间',
    }
  },
}

//获取操作人列表
function handleUserList(username) {
  return new Promise(async (resolve, reject) => {
    const { data, success } = await queryUserList({
      username,
      current: 1,
      size: 20,
    });
    if (success) {
      const newArr = [];
      data.map(item => {
        newArr.push({
          label: item.name ? `${item.name}（${item.account}）` : item.account,
          value: item.id,
        })
      })
      resolve(newArr);
    } else {
      reject();
    }
  })
}

//处理表单数据
export const getFormItem = (optionsData) => {
  const arr = [];
  Object.keys(formItemData).map(v => {
    if (optionsData && optionsData[v]) {
      formItemData[v].elConfig.options = optionsData[v];
    }
    arr.push(formItemData[v]);
  })
  return arr;
}

//数据获取方式
export const srcMap = {
  0: "手动录入",
  1: "接口获取",
  2: "仓储APP登记",
  3: "售后扫描登记"
};